﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections;

namespace QuickMAP.Code
{
    public class FileSortComparer : IComparer
    {
        public int SortType { set; get; }

        #region IComparer 成员

        public int Compare(object x, object y)
        {
            try
            {
                string sX = x.ToString(), sY = y.ToString();
                sX = sX.Substring(sX.LastIndexOf("\\") + 1, sX.Length - sX.LastIndexOf("\\") - 1);
                sY = sY.Substring(sY.LastIndexOf("\\") + 1, sY.Length - sY.LastIndexOf("\\") - 1);
                Regex regex = new Regex("^[0-9]+");
                int iX = 0, iY = 0;
                int.TryParse(regex.Match(sX).Value, out iX);
                int.TryParse(regex.Match(sY).Value, out iY);
                if (iX == 0 && iY == 0)
                {
                    return 0;
                }
                else
                {
                    if (SortType > 0)
                    {
                        return iX - iY;
                    }
                    else
                    {
                        return iY - iX;
                    }
                }
            }
            catch (Exception)
            {
                return 0;
            }
        }

        #endregion
    }
}
